<?php
/**
 * GeoContexter
 * @link http://code.google.com/p/geocontexter/
 * @package GeoContexter
 */

/**
 * Manage project users
 *
 * @package GeoContexter
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @author Armand Turpel <geocontexter@gmail.com>
 * @version $Rev: 844 $ / $LastChangedDate: 2011-03-24 09:50:17 +0100 (Do, 24 Mrz 2011) $ / $Author: armand.turpel $
 */

namespace Geocontexter\Controller;

use Zend\View\Model\ViewModel;
use Core\Controller\AbstractController;

class ProjectUsersController extends AbstractController
{
    public function init()
    {
        $this->initView( 'geocontexter/project-users/index.phtml' );

        $this->id_project = $this->id_project = $this->params()->fromRoute('id_project',false);

        if ((null === $this->id_project) || (0 == $this->id_project)) {
            return $this->error( '0 or no id_project request parameter defined.', __file__, __line__ );
        }

        $this->view->project_result = array();
        $this->view->users_result   = array();
        $this->view->id_project  = $this->id_project;
        $this->view->partialData = array('id_project'  => $this->id_project,
                                         'active_page' => 'project_users');
        $this->view->error       = array();
    }


    public function indexAction()
    {
        $this->init_data();
        return $this->view;
    }

    /**
     * update project
     */
    public function updateAction()
    {
        // check on cancel action
        //
        $cancel    = $this->request->getPost()->cancel;

        if ($cancel !== null) {
            return $this->redirect()->toUrl($this->getAdminBaseUrl() . '/project-edit/index/id_project/' . $this->id_project);
        }

        $id_project = $this->request->getPost()->id_project;

        $params  = array();

        $error = array();

        $params['id_project'] = $id_project;

        $remove_project_user = $this->request->getPost()->delete_id_user;

        if (count($error) > 0) {
            $this->init_data();
            return $this->indexAction();
        }

        // remove project users
        //
        if (($remove_project_user !== null) && is_array($remove_project_user)) {

            $ProjectRemoveUsers = $this->CoreModel('ProjectRemoveUsers');
            $params['id_user'] = $remove_project_user;
            $result = $ProjectRemoveUsers->run( $params );
        }

        return $this->redirect()->toUrl($this->getAdminBaseUrl() . '/project-edit/index/id_project/' . $id_project);

    }

    public function init_data()
    {
        // get current project
        //
        $ProjectGet = $this->CoreModel('ProjectGet');
        $params     = array('id_project' => $this->id_project);

        $result_project  = $ProjectGet->run( $params );

        $this->renderer->headTitle('Users of project ' . $result_project['title'], 'PREPEND');
        $this->view->project_result = $result_project;

        $UserGetProjectRelated = $this->CoreModel('UserGetProjectRelated');

        $params                = array('id_project'    => $this->id_project,
                                       'system_serial' => true);

        $this->view->users_result = $UserGetProjectRelated->run( $params );

        $this->register_model_callbacks();
    }

    /**
     * Register of model callback classes
     *
     *
     *
     * @param object $session
     */
    private function register_model_callbacks()
    {
        $this->ModelCallback = $this->CoreModel('ModelCallback');
        $this->ModelCallback->session = $this->sessionGet();

        // url to reload after a model callback was done
        //
        $this->opener_url = $this->getAdminBaseUrl().'/project-users/index/id_project/'.$this->id_project;

        //
        // add project users
        //
        $params_users =
                  array('model_class'         => 'ProjectAddUsers',
                        'model_class_methode' => 'run',
                        'model_field'         => 'id_user',
                        'id_name'             => 'id_project',
                        'id_value'            => $this->id_project,
                        'input_type'          => 'checkbox',
                        'opener_url'          => $this->opener_url);

        $callback_number  = $this->ModelCallback->register( $params_users );

        $this->view->project_users_callback_number = $callback_number;
    }

}

